package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.domain.Label;
import com.juyu.miliao.api.domain.LikeLabel;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.UserLikeLabel;
import com.juyu.miliao.api.service.LabelService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户评论标签
 *
 * @author wbf
 * 2018年6月26日
 */
@RestController
@RequestMapping("/lable")
@Api(tags = "标签", value = "标签")
public class LabelController {

    @Resource
    LabelService labelService;


    @GetMapping
    @ApiOperation(value = "获取评价标签列表")
    public List<Label> getLabelList(@ApiParam(value = "0-女 1-男 2-全部") Integer sex) {
        if (sex != null) {
            if (!(sex == 0 || sex == 1)) {
                sex = null;
            }
        }

        return labelService.getLabelList(sex);
    }

    /**
     * 寻寻1.5.2需求获取全部标签
     * @param sex
     * @return
     */
    @GetMapping("/list/{sex}")
    @ApiOperation(value = "获取评价标签列表")
    public List<Label> getRedomLabelList(Authentication auth,@ApiParam(value = "0-女 1-男 2-全部") @PathVariable("sex") Integer sex) {
        if (sex != null) {
            if (!(sex == 0 || sex == 1)) {
                sex = null;
            }
        }

        Subject subject = (Subject) auth.getPrincipal();
         List<Label> redomLabelList = labelService.getRedomLabelList(sex, subject.getId());

        return redomLabelList;

    }

    /**
     * 1.4.3 需求保存主播自我评价标签
     * @param ids
     * @param auth
     */
    @PostMapping("/save")
    public void insetLabel(@RequestParam String ids, Authentication auth){
        List<Long> list = CommonUtil.changeIds(ids);
        if (list.isEmpty() || list.size() > 3) {
            throw new ConsumeRuntimeException("标签不能为空且不大于3个");
        }
        Subject subject = (Subject) auth.getPrincipal();
        labelService.insertLabelForUserMeLabel(subject.getId(),list);
    }

    /**
     * 1.4.3 需求 获取主播自我评价标签
     * @param auth
     * @return
     */
    @PostMapping("/hostLabel")
    public List<Label> getUserLable(Authentication auth){
        Subject subject = (Subject) auth.getPrincipal();
       return labelService.getLabelForMeLabel(subject.getId());
    }

    /**
     * 1.4.3 需求 获取该标签列表
     * @param labelId
     * @return
     */
    @PostMapping("/hostList")
    public List<User> getUserForLabel(@RequestParam String labelId){
        return labelService.getUserForLabel(labelId);
    }

    @GetMapping("/{userId}")
    @ApiOperation(value = "获取用户标签列表")
    public List<Label> getLabelCountByUserId(@PathVariable("userId") Long userId) {
        return labelService.getLabelCountByUserId(userId);
    }

    @PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "评价用户标签", notes = "聊天结束评价界面",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void insertUserLabel(@RequestParam("userId") Integer userId, @RequestParam("label") String label) {
        labelService.insertUserLabel(userId,label);
    }

    @PostMapping("/userLabel")
    public void saveUserLabel(@RequestParam long insertUserId, @RequestParam String label,Authentication auth) {
        Subject subject = (Subject) auth.getPrincipal();
        labelService.insertUserAssessLabel(subject.getId(),insertUserId,label);
    }

    @PostMapping(value = "/user-like"
           , consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    @ApiOperation(value = "保存用户私人喜欢的标签",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void saveUserPersonLike(@ApiParam(value = "标签id 英文 ,逗号隔开", required = true) @RequestParam("ids") String ids,
                                   Authentication auth) {
        List<Long> list = CommonUtil.changeIds(ids);
        if (list.isEmpty() || list.size() > 3) {
            throw new ConsumeRuntimeException("标签不能为空且不大于3个");
        }

        Subject subject = (Subject) auth.getPrincipal();
        Long userId = subject.getId();
        labelService.saveUserLikeLabel(userId, list);
    }

    @GetMapping(value = "/user-like")
    @ApiOperation(value = "获取私人标签list")
    public List<LikeLabel> listUserLike() {
        return labelService.listUserLikeLabel();
    }

}
